/*
 * @Author: yangrongxin
 * @Date: 2021-04-13 16:06:01
 * @LastEditors: guoju
 * @LastEditTime: 2021-12-24 15:24:25
 */
import 'reflect-metadata';
import { SPLIT_OPERATOR, CONTEXT_PATH, REQUEST_TYPE } from '../const';
import { RequestType } from '../enum';

/**
 * @description 工具类函数集合
 * @author yangrongxin
 * @date 2021-04-13
 * @class RequestUtilsBase
 */
class RequestUtilsBase {
  [props: string]: any; // 因为要注入其他内容 所以参数设置为所有属性值都允许注入

  /**
   * @description 生成字符串 模块名称操作符属性名称
   * @author yangrongxin
   * @date 2021-04-13
   * @param {*} className 原型对象名称
   * @param {*} propertyName 原型对象的静态属性名称
   * @returns
   */
  static toString(className: string, propertyName: string): string {
    return `${className}${SPLIT_OPERATOR}${propertyName}`;
  }

  /**
   * @description 将字符串返还回一个数组 [className, propertyName]
   * @author yangrongxin
   * @date 2021-04-13
   * @param {*} apiName 用于解析封装好的方法
   * @returns
   */
  static toArray(apiName: string): string[] {
    return apiName?.split(SPLIT_OPERATOR);
  }

  /**
   * @description 将字符串返回为一个对象 {apiName, url}
   * @author yangrongxin
   * @date 2021-04-13
   * @param {string} api
   * @returns {Object}
   */
  static toObject(api: string): {
    apiName: string;
    url: string;
    method: RequestType;
  } {
    const [apiName, url] = RequestUtilsBase.toArray(api);
    const contextPath = Reflect.getMetadata(CONTEXT_PATH, RequestUtilsBase[apiName]) || '';
    const method = Reflect.getMetadata(REQUEST_TYPE, RequestUtilsBase[apiName], url) || RequestType.GET;

    return {
      apiName,
      url: `${contextPath}${url}`,
      method,
    };
  }

  /**
   * @description 获得地址中对应的url名称
   * @author yangrongxin
   * @date 2021-04-13
   * @param {string} api
   * @returns {string}
   */
  static getUrl(api: string): string {
    return RequestUtilsBase.toObject(api)?.url;
  }
}

const { toString, toObject, getUrl } = RequestUtilsBase;

export default RequestUtilsBase;

export { toString, toObject, getUrl };

export const optionLabel = [
  'A',
  'B',
  'C',
  'D',
  'E',
  'F',
  'G',
  'H',
  'I',
  'J',
  'k',
];

export const optionChildLabel = [
  'a',
  'b',
  'c',
  'd',
  'e',
  'f',
  'g',
  'h',
  'i',
  'j',
  'k',
];